<!-- src/layout/AdminLayout.vue -->
<template>
  <el-container style="height: 100vh">
    <!-- 1. 侧边栏 -->
    <el-aside :width="isCollapse ? '64px' : '240px'" class="side-bar">
      <div class="logo-container">
        <img
          v-if="!isCollapse"
          src="@/assets/logo.svg"
          alt="Logo"
          class="logo-full"
        />
        <img v-else src="@/assets/logo.svg" alt="Logo" class="logo-icon" />
      </div>
      <el-scrollbar>
        <el-menu
          :collapse="isCollapse"
          :default-active="activeMenu"
          class="el-menu-vertical"
          background-color="#001529"
          text-color="#fff"
          active-text-color="#409EFF"
          :unique-opened="true"
          @select="onMenuSelect"
        >
          <el-menu-item index="Dashboard" class="menu-item">
            <el-icon><View /></el-icon>
            <template #title>
              <span class="menu-title">首页</span>
            </template>
          </el-menu-item>
          
          <el-sub-menu index="1" class="menu-item">
            <template #title>
              <el-icon><UserFilled /></el-icon>
              <span class="menu-title">用户管理</span>
            </template>
            <el-menu-item index="Users" class="sub-menu-item">用户列表</el-menu-item>
            <el-menu-item index="Roles" class="sub-menu-item">角色管理</el-menu-item>
            <el-menu-item index="RolePermissions" class="sub-menu-item">权限管理</el-menu-item>
          </el-sub-menu>

          <el-sub-menu index="2" class="menu-item">
            <template #title>
              <el-icon><Goods /></el-icon>
              <span class="menu-title">商品管理</span>
            </template>
            <el-menu-item index="Products" class="sub-menu-item">商品列表</el-menu-item>
            <el-menu-item index="Categories" class="sub-menu-item">分类管理</el-menu-item>
            <el-menu-item index="Tags" class="sub-menu-item">标签管理</el-menu-item>
            <el-menu-item index="Brands" class="sub-menu-item">品牌管理</el-menu-item>
           
            <el-menu-item index="SKU" class="sub-menu-item">SKU管理</el-menu-item>
            <el-menu-item index="SPU" class="sub-menu-item">SPU管理</el-menu-item>
          </el-sub-menu>

          <el-sub-menu index="3" class="menu-item">
            <template #title>
              <el-icon><Shop /></el-icon>
              <span class="menu-title">门店管理</span>
            </template>
            <el-menu-item index="Store" class="sub-menu-item">门店管理</el-menu-item>
            <el-menu-item index="StoreAddresses" class="sub-menu-item">门店地址管理</el-menu-item>
            <el-menu-item index="Riders" class="sub-menu-item">骑手管理</el-menu-item>
          </el-sub-menu>

          <el-sub-menu index="4" class="menu-item">
            <template #title>
              <el-icon><Document /></el-icon>
              <span class="menu-title">订单管理</span>
            </template>
            <el-menu-item index="Orders" class="sub-menu-item">订单列表</el-menu-item>
          </el-sub-menu>

          <el-sub-menu index="5" class="menu-item">
            <template #title>
              <el-icon><Setting /></el-icon>
              <span class="menu-title">系统管理</span>
            </template>
            <el-menu-item index="ActivityLogs" class="sub-menu-item">日志管理</el-menu-item>
            <el-menu-item index="Banners" class="sub-menu-item">轮播图管理</el-menu-item>
          </el-sub-menu>
        </el-menu>
      </el-scrollbar>

      <!-- 侧边栏折叠/展开按钮 -->
      <div class="collapse-btn" @click="toggleCollapse">
        <el-icon>
          <component :is="isCollapse ? Expand : Fold" />
        </el-icon>
      </div>
    </el-aside>

    <!-- 2. 右侧：头部 + 路由视图 -->
    <el-container class="main-container">
      <!-- 顶部栏 -->
      <el-header class="header-bar">
        <div class="header-left">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item to="/">首页</el-breadcrumb-item>
            <el-breadcrumb-item>{{ currentRouteTitle }}</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <div class="header-right">
          <el-tooltip content="全屏" placement="bottom">
            <el-icon class="header-icon" @click="toggleFullScreen">
              <FullScreen />
            </el-icon>
          </el-tooltip>
          <el-dropdown trigger="click">
            <div class="user-info">
              <el-avatar :size="32" :src="avatarUrl" />
              <span class="username">{{ username }}</span>
              <el-icon><ArrowDown /></el-icon>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item>
                  <el-icon><User /></el-icon>
                  个人信息
                </el-dropdown-item>
                <el-dropdown-item>
                  <el-icon><Setting /></el-icon>
                  系统设置
                </el-dropdown-item>
                <el-dropdown-item divided @click="logout2">
                  <el-icon><SwitchButton /></el-icon>
                  退出登录
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <!-- 主体内容区域 -->
      <el-main class="main-content">
        <router-view v-slot="{ Component }">
          <transition name="fade-transform" mode="out-in">
            <component :is="Component" />
          </transition>
        </router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { ref, computed, watch } from "vue";
import { useRoute } from "vue-router";
import { ElMessageBox, ElMessage } from "element-plus";
import router from "@/router";
import { useTokenStore } from "@/stores/useTokenStore";
import { logout } from "@/api/auth";

// 引入需要的图标
import {
  View,
  UserFilled,
  Goods,
  Shop,
  Setting,
  ArrowDown,
  Fold,
  Expand,
  FullScreen,
  User,
  SwitchButton,
  Document
} from "@element-plus/icons-vue";

const username = ref(localStorage.getItem("username") || "管理员");
const avatarUrl = ref("https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png");
const tokenStore = useTokenStore();

// 侧边栏折叠状态
const isCollapse = ref(false);

// 当前激活菜单
const route = useRoute();
const activeMenu = ref(route.name || "Dashboard");

// 监听路由变化
watch(
  () => route.name,
  (newName) => {
    activeMenu.value = newName;
  }
);

// 菜单选择
function onMenuSelect(key) {
  router.push({ name: key });
}

// 计算当前路由标题
const currentRouteTitle = computed(() => {
  return route.meta.title || "";
});

// 切换侧边栏折叠状态
function toggleCollapse() {
  isCollapse.value = !isCollapse.value;
}

// 切换全屏
function toggleFullScreen() {
  if (!document.fullscreenElement) {
    document.documentElement.requestFullscreen();
  } else {
    document.exitFullscreen();
  }
}

// 退出登录
function logout2(){
  ElMessageBox.confirm("确定要退出登录吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then( async () => {
      try {
        // 先清除本地token
        tokenStore.clearToken();
        localStorage.removeItem("username");
        
        // 再调用后端登出接口
        await logout();
        
        ElMessage.success("退出登录成功");
        // 最后跳转到登录页
        router.replace("/login");
      } catch (error) {
        console.error("退出登录失败", error);
        // 即使API调用失败，也确保用户被登出
        router.replace("/login");
      }
    })
    .catch(() => {
      ElMessage.info("已取消退出登录");
    });
}
</script>

<style scoped>
/* 侧边栏样式 */
.side-bar {
  background-color: #001529;
  position: relative;
  transition: width 0.3s;
  box-shadow: 2px 0 6px rgba(0, 21, 41, 0.35);
}

.logo-container {
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: #002140;
}

.logo-full {
  height: 32px;
  transition: all 0.3s;
}

.logo-icon {
  height: 32px;
  transition: all 0.3s;
}

/* 菜单样式 */
.el-menu-vertical {
  border-right: none;
}

.menu-item {
  margin: 4px 0;
}

.menu-title {
  font-size: 14px;
  margin-left: 8px;
}

.sub-menu-item {
  padding-left: 48px !important;
}

/* 折叠按钮 */
.collapse-btn {
  position: absolute;
  bottom: 20px;
  left: 0;
  right: 0;
  text-align: center;
  cursor: pointer;
  padding: 12px 0;
  color: #fff;
  transition: all 0.3s;
}

.collapse-btn:hover {
  background: rgba(255, 255, 255, 0.1);
}

/* 顶部栏 */
.header-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #fff;
  padding: 0 24px;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  height: 64px;
}

.header-left {
  display: flex;
  align-items: center;
}

.header-right {
  display: flex;
  align-items: center;
}

.header-icon {
  margin-right: 15px;
  font-size: 20px;
  cursor: pointer;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 0 8px;
  height: 48px;
  border-radius: 24px;
  transition: background-color 0.3s;
}

.user-info:hover {
  background-color: #f5f7fa;
}

.username {
  font-size: 14px;
  color: #666;
}

/* 主体内容 */
.main-container {
  display: flex;
  flex-direction: column;
  background-color: #f0f2f5;
}

.main-content {
  padding: 24px;
  overflow-y: auto;
  height: calc(100vh - 64px);
}

/* 路由切换动画 */
.fade-transform-enter-active,
.fade-transform-leave-active {
  transition: all 0.3s;
}

.fade-transform-enter-from {
  opacity: 0;
  transform: translateX(-30px);
}

.fade-transform-leave-to {
  opacity: 0;
  transform: translateX(30px);
}

/* 滚动条样式 */
:deep(.el-scrollbar__bar.is-horizontal) {
  display: none;
}

:deep(.el-scrollbar__bar.is-vertical) {
  width: 6px;
}

:deep(.el-scrollbar__thumb) {
  background-color: rgba(255, 255, 255, 0.2);
}

/* 下拉菜单样式 */
:deep(.el-dropdown-menu__item) {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
}

:deep(.el-dropdown-menu__item .el-icon) {
  margin-right: 4px;
}
</style>
  